package com.likou;

public class q31 {

    public void nextPermutation(int[] nums) {
        int i;
        for (i = nums.length - 1; i >= 0; i--) {
            if (i + 1 < nums.length && nums[i] < nums[i + 1]) {
                for (int j = nums.length - 1; j >= i + 1; j--) {
                    if (nums[j] > nums[i]) {
                        int temp = nums[i];
                        nums[i] = nums[j];
                        nums[j] = temp;
                        break;
                    }
                }
                break;
            }
        }

        int left = i + 1;
        int right = nums.length - 1;
        while (left < right) {
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
            left++;
            right--;
        }

    }

}
